package com.plian.system.service.sys.timing;

import com.plian.Tools.UuidUtil;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.config.exception.MyRuntimeException;
import com.plian.system.dao.sys.timing.TimingDao;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.ObjResult;
import com.plian.system.quartz.QuartTask;
import com.plian.system.quartz.QuartzManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class TimingService {
    @Autowired
    TimingDao timingDao;
    @Autowired
    QuartzManager quartzManager;
    @Autowired
    QuartTask quartTask;

    public ObjResult save(PageData pd){
        try {
            pd.put("id", UuidUtil.get32UUID());
            pd.put("status","0");
            ObjResult objResult=new ObjResult(CommonCode.SUCCESS);
            timingDao.save(pd);
            return objResult;
        } catch (Exception e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
            throw new MyRuntimeException();
        }
    }
    public ObjResult edit(PageData pd){
        try {
            ObjResult objResult=new ObjResult(CommonCode.SUCCESS);
            timingDao.edit(pd);
            return objResult;
        } catch (Exception e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
            throw new MyRuntimeException();
        }
    }
    public ObjResult delid(PageData pd){
        try {
            ObjResult objResult=new ObjResult(CommonCode.SUCCESS);
            List<String> ids= (List<String>) pd.get("ids");
            timingDao.del_ids(ids);
            return objResult;
        } catch (Exception e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
            throw new MyRuntimeException();
        }
    }
    public ListResult list(PageData pd){
        try {
            ListResult listResult=new ListResult(CommonCode.SUCCESS);
           listResult.setList(timingDao.list(pd));
            return listResult;
        } catch (Exception e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
            throw new MyRuntimeException();
        }
    }
    public ObjResult findid(PageData pd){
        try {
            ObjResult objResult=new ObjResult(CommonCode.SUCCESS);
            PageData p=timingDao.findbyid(pd.getString("id"));
            objResult.setPageData(p);
            return objResult;
        } catch (Exception e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
            throw new MyRuntimeException();
        }
    }
    @Transactional(rollbackFor=RuntimeException.class)
    public ObjResult timing_strat(PageData pd){
        try {
            pd=timingDao.findbyid(pd.getString("id"));
            String status=pd.getString("status");
            if("1".equals(status)){
                ObjResult objResult=new ObjResult(CommonCode.ERRORPARAMETER);
                objResult.setMessage("该任务已启用!!!");
                return objResult;
            }
            String fun=pd.getString("fun");
            String timingType=pd.getString("timingType");
            String timingMonth=pd.getString("timingMonth");
            String timingDay=pd.getString("timingDay");
            String timingHour=pd.getString("timingHour");
            String timmingMinutes=pd.getString("timmingMinutes");
            quartTask.task(fun,timingType,timingMonth,timingDay,timingHour,timmingMinutes);
            pd.put("status","1");
            timingDao.edit(pd);
            ObjResult objResult=new ObjResult(CommonCode.SUCCESS);
            return objResult;
        }catch (MyRuntimeException e){
            throw e;
        }
        catch (Exception e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
            throw new MyRuntimeException();
        }
    }




    @Transactional(rollbackFor=RuntimeException.class)
    public ObjResult timing_end(PageData pd){
        try {
            PageData p=timingDao.findbyid(pd.getString("id"));
            quartTask.delTask(p.getString("fun"));
            p.put("status","0");
            timingDao.edit(p);
            return new ObjResult(CommonCode.SUCCESS);
        } catch (Exception e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
           throw  new MyRuntimeException();
        }
    }
}
